<template>
  <WsmTable
    ref="table"
    load-url="/dbms-server/base/sqlconfig/page"
    :change-load="true"
    :offset-height="80"
    v-model="queryFormType"
  >
    <template #title>{{ t('dbms.design.sqlDesign.title') }}</template>
    <template #button="{ row }">
      <el-button type="primary" v-hasPermi="'add'" @click="addForm.show()">{{
        t('common.add')
      }}</el-button>
      <template v-if="row">
        <el-button v-hasPermi="'update'" @click="update(row.id)" type="primary">{{
          t('common.update')
        }}</el-button>
        <el-button v-hasPermi="'delete'" @click="del(row.id)" type="danger">{{
          t('common.delete')
        }}</el-button>
      </template>
    </template>
    <template #column>
      <el-table-column
        prop="activeVersion"
        :label="t('dbms.design.sqlDesign.activeVersion')"
        width="100"
      />
      <el-table-column prop="sqlId" :label="t('dbms.design.sqlDesign.sqlId')" width="340" />
      <el-table-column
        prop="serverName"
        :label="t('dbms.design.sqlDesign.serverName')"
        width="180"
      />
      <el-table-column prop="name" :label="t('dbms.design.sqlDesign.name')" />
    </template>
  </WsmTable>
  <WsmForm
    ref="addForm"
    v-model="addData"
    submit-url="/dbms-server/base/sqlconfig/add"
    label-width="100px"
    :init-data="initData"
    :title="t('dbms.design.sqlDesign.addTitle')"
    @success="flushed"
  >
    <template #default="{ form }">
      <el-row>
        <el-col :span="12">
          <el-form-item prop="sqlId" :label="t('dbms.design.sqlDesign.sqlId')">
            <el-input v-model="form.sqlId" :placeholder="t('dbms.design.sqlDesign.sqlId')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="name" :label="t('dbms.design.sqlDesign.name')">
            <el-input v-model="form.name" :placeholder="t('dbms.design.sqlDesign.name')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item prop="entSql" :label="t('dbms.design.sqlDesign.entSql')">
            <CodeInput
              v-model="form.entSql"
              :code-style="{ height: '400px', width: '100%' }"
              code="sql"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>

  <WsmForm
    ref="updateForm"
    v-model="updateData"
    load-url="/dbms-server/base/sqlconfig/get"
    label-width="100px"
    submit-url="/dbms-server/base/sqlconfig/update"
    :title="t('dbms.interLang.updateTitle')"
    @success="flushed"
  >
    <template #default="{ form }">
      <el-row>
        <el-col :span="24">
          <el-form-item
            prop="name"
            :label="t('dbms.design.sqlDesign.name') + '(' + form.sqlId + ')'"
          >
            <el-input v-model="form.name" :placeholder="t('dbms.design.sqlDesign.name')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item prop="entSql" :label="t('dbms.design.sqlDesign.entSql')">
            <CodeInput
              v-model="form.entSql"
              :code-style="{ height: '400px', width: '100%' }"
              code="sql"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>
</template>
<script setup lang="ts">
import { WsmTable } from '@/components/WsmTable'
import { WsmForm } from '@/components/WsmForm'
import { QueryFormType } from '@/components/WsmTable/src/types'
import { delApi } from '@/api/dbms/sqlconfig'
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { FormDataType } from '@/components/WsmForm/src/types'
import { CodeInput } from '@/components/CodeMirror'

const { t } = useI18n()

const appName = defineModel<string>()
//表格对象
const table = ref()

//新增表单对象
const addForm = ref()
const initData = {
  serverName: appName.value
}
const addData: FormDataType = {
  sqlId: '',
  name: '',
  serverName: '',
  activeVersion: 0,
  entSql: ''
}

//修改表单对象
const updateForm = ref()
const updateData: FormDataType = {
  id: '',
  sqlId: '',
  name: '',
  activeVersion: 0,
  entSql: ''
}

//查询定义
const queryFormType: QueryFormType = {
  serverName: {
    value: appName.value,
    clear: false
  }
}

const update = (id: string) => {
  updateForm.value.show({ id })
}

const del = async (id: string) => {
  ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel'),
    type: 'warning'
  }).then(async () => {
    const rs = await delApi({ id })
    if (rs.success) {
      table.value.load(1)
    }
  })
}

const flushed = () => {
  table.value.load(1)
}
</script>

<style scoped></style>
